<?php

/**
 * 生产查询
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');

quanli_require('module/base/bill/billSearch.class.php');

class ajax_productionSearch {


    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //列表
                    $this->productionList();
                    break;

                case 2: //打印数据
                    $this->productionScanPrint();
                    break;
                case 3: //城市=>配送站联动
                    $this->linkageDistribution();
                    break;
            }
        }
    }


    /*
     * 列表
     *
     * */
    public function productionList () {

        global $G_PGLOB;

        //查找订单主表数据
        $bill_array = $this->searchBillInfo( $_REQUEST ) ;

//        echo '<pre>';
//        print_r($bill_array);
//        exit;
        //查询结果
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($bill_array) {
            foreach ($bill_array as $key=>&$goodsInfo) {

                //订单标示
                if($goodsInfo['is_urgent'] == 1){
                    $is_urgent = "加急";
                }else{
                    $is_urgent = "";
                }

                //订单类型
                switch ($goodsInfo['billType']) {
                    case '1':
                        $billType = "正常订单";
                        break;
                    case '2':
                        $billType = "试吃订单";
                        break;
                    case '3':
                        $billType = "大客户订单";
                        break;
                    case '4':
                        $billType = "赠送订单";
                        break;
                    case '5':
                        $billType = "赔偿订单";
                        break;
                    case '6':
                        $billType = "内部订单";
                        break;
                }

                $result_array['rows'][] = array(
                    'id' =>$goodsInfo['bill_no'],
                    'cell' => array(
                        $key+1,
                        $goodsInfo['cityID'],
                        $is_urgent,
                        $billType,
                        $goodsInfo['send_date'],
                        $goodsInfo['send_start_time'],
                        $goodsInfo['send_end_time'],
                        $goodsInfo['createTime'],
                        $goodsInfo['print_num'],
                        $goodsInfo['bill_no'],
                        $goodsInfo['distribution_name'],
                        $goodsInfo['product_code'],
                        $goodsInfo['goods_name'],
                        $goodsInfo['goods_spec'],
                        $goodsInfo['goods_amount'],
                        $goodsInfo['tableware_amount'],
                        $goodsInfo['birthday_card'],
                        $goodsInfo['segmentation']?'':$goodsInfo['segmentation'],
                        $goodsInfo['goods_remark']
                    )
                );
            }
        }
        echo json_encode($result_array);

    }


    /**
     * 查询订单主表信息
     * @param array $arg_array
     * @return null|array
     */
    public function searchBillInfo( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //订单号
        if (isset($arg_array['bill_no']) && !empty($arg_array['bill_no'])) {
            $sql .= ' AND bill_no = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['bill_no'];
        }

        //订单编号
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['bill_code'];
        }

        //订单商品流水号
        if (isset($arg_array['bill_goods_no']) && is_numeric($arg_array['bill_goods_no'])) {
            $sql .= ' AND bill_goods_no = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bill_goods_no'];
        }

        //城市ID
        if (isset($arg_array['cityID']) && is_numeric($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        //订单类型
        if (isset($arg_array['billType']) && in_array($_REQUEST['billType'],array(1,2,3,4,5,6))) {
            $sql .= ' AND billType = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['billType'];
        }

        //配送日期
        if (isset($arg_array['send_date']) && !empty($arg_array['send_date'])) {
            $sql .= ' AND send_date = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_date'].':00';
        }

        //配送开始时间
        if (isset($arg_array['send_start_time']) && !empty($arg_array['send_start_time'])) {
            $sql .= ' AND send_start_time >= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_start_time'].':00';
        }

        //配送结束时间
        if (isset($arg_array['send_end_time']) && !empty($arg_array['send_end_time'])) {
            $sql .= ' AND send_end_time <= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_end_time'].':00';
        }

        //配送站ID
        if (isset($arg_array['distribution_id']) && is_numeric($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        //配送站名称
        if (isset($arg_array['distribution_name']) && !empty($arg_array['distribution_name'])) {
            $sql .= ' AND distribution_name = ?';
            $params_type .= 's';
            $params_array[] = encryptD($arg_array['distribution_name']);
        }

        //加急状态
        if (isset($arg_array['is_urgent']) && is_numeric($arg_array['is_urgent'])) {
            $sql .= ' AND is_urgent = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['is_urgent'];
        }

        //网购号
        if (isset($_REQUEST['goods_postID']) && is_numeric($arg_array['goods_postID'])) {
            $sql .= ' AND goods_postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['goods_postID'];
        }


        //商家编码
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称 /SKU / 商品ID  模糊查询
        if (isset($arg_array['sku']) && !empty($arg_array['sku'])) {
            $sql .= ' AND (goods_name like ? OR product_code =? OR goods_postID = ?) ';
            $params_type .= 'ssi';
            $params_array[] = '%'.$arg_array['sku'].'%';
            $params_array[] = $arg_array['sku'];
            $params_array[] = $arg_array['sku'];
        }

        //商品规格
        if (isset($arg_array['goods_spec']) && !empty($arg_array['goods_spec'])) {
            $sql .= ' AND goods_spec = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['goods_spec'];
        }

        //商品数量
        if (isset($arg_array['goods_amount']) && is_numeric($arg_array['goods_amount'])) {
            $sql .= ' AND goods_amount = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['goods_amount'];
        }

        //餐具数量
        if (isset($arg_array['tableware_amount']) && is_numeric($arg_array['tableware_amount'])) {
            $sql .= ' AND tableware_amount = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['tableware_amount'];
        }

        //生日牌
        if (isset($arg_array['birthday_card']) && !empty($arg_array['birthday_card'])) {
            if($arg_array['birthday_card'] == 1){ //状态为是 含生日牌
                $sql .= ' AND birthday_card IS NOT NULL';
            }else{
                $sql .= ' AND birthday_card IS NULL';
            }
        }

        //切分状态
        if (isset($arg_array['segmentation']) && is_numeric($arg_array['segmentation'])) {
            if($arg_array['segmentation'] == 1){ //状态为是 切分
                $sql .= ' AND segmentation >= ?';
            }else{
                $sql .= ' AND segmentation = ?';
            }
            $params_type .= 'i';
            $params_array[] = $arg_array['segmentation'];

        }

        //是否打印
        if (isset($arg_array['print_num']) && is_numeric($arg_array['print_num'])) {
            if($arg_array['print_num'] == 1){ //状态为是 打印
                $sql .= ' AND print_num >= ?';
            }else{
                $sql .= ' AND print_num = ?';
            }
            $params_type .= 'i';
            $params_array[] = $arg_array['print_num'];

        }

        //拓展字段
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_goods_product ' . $sql;

                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);


                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM bill_goods_product' . $sql;
            //return $search_sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array) {
                return null;
            }

            foreach ($temp_item_array as $bill) {


                $item_array[] = array(

                    'uuid' => $bill->uuid,
                    'creator' => $bill->creator,
                    'createTime' => $bill->createTime,
                    'bill_no' => $bill->bill_no,
                    'bill_code' => $bill->bill_code,
                    'bill_goods_no' => $bill->bill_goods_no,
                    'cityID' => $bill->cityID,
                    'cityID' => $bill->cityName,
                    'billType' => $bill->billType,
                    'send_date' => $bill->send_date,
                    'send_start_time' => $bill->send_start_time,
                    'send_end_time' => $bill->send_end_time,
                    'distribution_id' => $bill->distribution_id,
                    'distribution_name' => $bill->distribution_name,
                    'is_urgent' => $bill->is_urgent,
                    'goods_postID' => $bill->goods_postID,
                    'product_code' => $bill->product_code,
                    'goods_name' => $bill->goods_name,
                    'goods_spec' => $bill->goods_spec,
                    'goods_amount' => $bill->goods_amount,
                    'tableware_amount' => $bill->tableware_amount,
                    'birthday_card' => $bill->birthday_card,
                    'segmentation' => $bill->segmentation,
                    'goods_remark' => $bill->goods_remark,
                    'print_num' => $bill->print_num,
                    'print_operator' => $bill->print_operator,
                    'print_time' => $bill->print_time

                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }

    }

    /**
     *
     * 打印商品生产信息
     */
//   private function productionScanPrint(){

//        global $G_PGLOB;
//
//        $bill_goods_array = array(); //订单商品数组
//
//        //实例订单主表类
//        $billMaster = new billSearch($G_PGLOB->DBCA);
//
//        $_REQUEST['bill_status'] = 1; //查询确认状态订单
//
//        //查找订单主表数据
//        $bill_array = $billMaster->searchBillInfo( $_REQUEST ) ;
//
//        if($bill_array){
//            foreach($bill_array as &$master){
//                $arg_array = array(
//                    'bill_no' => $master['bill_no']
//                );
//                //查找订单goods表数据
//                $temp_goods_array = $billMaster->searchBillGoods( $arg_array ) ;
//
//                foreach($temp_goods_array as &$goods) {
//                    $bill_goods_array[] = $goods;
//                }
//
//            }
//        }
//
//        if(  $bill_goods_array  ){
//            $G_PGLOB->smarty->assign('bill_goods_array',$bill_goods_array);
//            $print_array[] = $G_PGLOB->smarty->fetch('production/productionScanPrint.tpl');
//            returnJsonData('ok',200,$print_array);
//        }else {
//            returnJsonData( '暂无数据',400);
//        }
//    }
//

    /*
     *
     * 城市=>配送站二级联动
     */
    public function linkageDistribution (){

        if (isset($_REQUEST['cityID']) && is_numeric($_REQUEST['cityID'])){
            $distributionMsg = get_distribution();

//            echo '<pre>';
//            print_r($distributionMsg);exit;
            if($distributionMsg){
                returnJsonData( '',200,$distributionMsg);
            }else{
                returnJsonData( '暂无配送站数据',400);
            }
        }
    }
}
